package v1

import (
	"fmt"
	"net/http"
	"time"
	"toy-car/models"
	"toy-car/pkg/e"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func MenuList(c *gin.Context) {
	data := make(map[string]interface{})
	code := e.SUCCESS

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": data,
	})
}

// @Tags Menu
// @Summary 新增菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysBaseMenu true "路由path, 父菜单ID, 路由name, 对应前端文件路径, 排序标记"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
// @Router /menu/base [post]
func MenuAddBase(c *gin.Context) {
	var menu models.SysBaseMenu

	_ = c.ShouldBindJSON(&menu)

	// todo 手动补充默认值？
	menu.CreatedOn = int(time.Now().UnixMicro())

	// todo 菜单提交数据的校验
	data := make(map[string]interface{})
	code := e.SUCCESS
	err := models.AddMenuBase(menu)
	if err != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": data,
	})
}

func MuneUpdateBase(c *gin.Context) {
	uid := com.StrTo(c.Param("id")).MustInt()
	var menu models.SysBaseMenu
	menu.ID = uid

	_ = c.ShouldBindJSON(&menu)
	// todo 菜单提交数据的校验
	data := make(map[string]interface{})
	code := e.SUCCESS
	err := models.UpdateMenuBase(menu)
	if err != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": data,
	})
}

// @Tags Menu
// @Summary 根据id获取菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "菜单id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/:id/base [get]
func MuneBaseGetByID(c *gin.Context) {
	pid := com.StrTo(c.Param("id")).MustInt64()

	// todo 菜单提交数据的校验
	code := e.SUCCESS
	err, menu := models.GetBaseMenuByID(pid)
	if err != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": menu,
	})
}

// @Tags Menu
// @Summary 删除菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "菜单id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /menu/deleteBaseMenu [post]
func MuneBaseDelete(c *gin.Context) {
	pid := com.StrTo(c.Param("id")).MustInt64()

	code := e.SUCCESS
	err := models.DeleteBaseMenu(pid)
	if err != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": true,
	})
}

// @Tags AuthorityMenu
// @Summary 获取指定角色menu
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetAuthorityId true "角色ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/authority/:id [post]
func MuneGetByAuthority(c *gin.Context) {
	pid := com.StrTo(c.Param("id")).MustInt64()

	code := e.SUCCESS
	err, menus := models.GetMenuAuthority(pid)
	if err != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": menus,
	})
}

// @Tags AuthorityMenu
// @Summary 增加menu和角色关联关系
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.AddMenuAuthorityInfo true "角色ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
// @Router /menu/addMenuAuthority [post]
func MuneAuthorityAdd(c *gin.Context) {
	var authorityMenu models.AddMenuAuthorityInfo
	_ = c.ShouldBindJSON(&authorityMenu)

	pid := c.Param("id")

	var menuAuthority []models.SysBaseMenu
	for _, v := range authorityMenu.Menus {
		t := models.SysBaseMenu{}
		t.ID = v
		menuAuthority = append(menuAuthority, t)
	}

	fmt.Println(menuAuthority)
	fmt.Println(pid)
	code := e.SUCCESS
	err := models.AddMenuAuthority(menuAuthority, pid)
	if err != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": true,
	})
}

// @Tags AuthorityMenu
// @Summary 获取用户动态路由
// @Security ApiKeyAuth
// @Produce  application/json
// @Param data body request.Empty true "空"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/getBaseMenuTree [post]
func BaseMenuTree(c *gin.Context) {
	code := e.SUCCESS
	err, menus := models.GetBaseMenuTree()
	if err != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": menus,
	})
}
